using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FetCo.Data;
using ADESSA.Business;
using ADESSA.Entities;
using FetCo.Framework;
using FetCo.Web;

public partial class Admin_ProductbuyList : System.Web.UI.Page
{
    #region Region
    public int NoCountStart
    {
        get { return Convert.ToInt32(ViewState["NoCountStart"]); }
        set { ViewState["NoCountStart"] = value; }
    }
    protected string hidColSort
    {
        get
        {
            object o = ViewState["hidColSort"];
            if (o == null) return "[" + ProductBuy._Table + "].[" + ProductBuyColumns.Order + "]";
            return o.ToString();
        }
        set
        {
            ViewState["hidColSort"] = value;
        }
    }
    public string Title
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["Title"] == null)
                return "";
            else
                return HttpContext.Current.Request.QueryString["Title"];
        }
    }
    public string KeyWord
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["KeyWord"] == null)
                return "";
            else
                return HttpContext.Current.Request.QueryString["KeyWord"];
        }
    }
    public long CatPro
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["CatPro"] == null)
                return long.MinValue;
            else
                return long.Parse(HttpContext.Current.Request.QueryString["CatPro"]);
        }
    }
    public long SubCatPro
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["SubCatPro"] == null)
                return long.MinValue;
            else
                return long.Parse(HttpContext.Current.Request.QueryString["SubCatPro"]);
        }
    }
    public long SubSubCatPro
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["SubSubCatPro"] == null)
                return long.MinValue;
            else
                return long.Parse(HttpContext.Current.Request.QueryString["SubSubCatPro"]);
        }
    }
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
            return;
        LoadCurent();
        LoadCategoryPro();
        txtSearch.Value = this.Title;
        LoadData();
    }
    #region Load dropdownlist here
    private void LoadCategoryPro()
    {

        ddlCat.Items.Clear();
        ddlCat.Items.Add(new ListItem("- - - -Select Category- - - -", "0"));
        ddlProSubCat.Items.Clear();
        ddlProSubCat.Items.Add(new ListItem("- - - -Select SubCategory- - - -", "0"));
        ddlProSubSubCat.Items.Clear();
        ddlProSubSubCat.Items.Add(new ListItem("- - - -Select  SubSubCategory- - - -", "0"));
        Criteria cri = new Criteria();
        cri.add("[" + ProductCat._Table + "].[" + ProductCatColumns.CatLevel + "]", 0, Condition.EQUAL, Condition.AND);
        ProductCatCollection _ProductCatCollection = ProductCatService.GetProductCatListByParam("[" + ProductCat._Table + "].[" + ProductCatColumns.SortOrder + "]", OrderDirection.ASC, cri.Criter);
        if (_ProductCatCollection.Count > 0)
        {
            foreach (ProductCat _ProductCat in _ProductCatCollection)
            {
                ddlCat.Items.Add(new ListItem(_ProductCat.Name, _ProductCat.ProCateID.ToString()));
            }
        }
    }
    private void LoadSubCatPro()
    {
        ddlProSubSubCat.Items.Clear();
        ddlProSubSubCat.Items.Add(new ListItem("- - - -Select SubSubCategory- - - -", "0"));
        Criteria cri = new Criteria();
        cri.add("[" + ProductCat._Table + "].[" + ProductCatColumns.CatLevel + "]", 1, Condition.EQUAL, Condition.AND);
        cri.add("[" + ProductCat._Table + "].[" + ProductCatColumns.Parrentid + "]", Utility.ParseToBigint(ddlCat.SelectedValue), Condition.EQUAL, Condition.AND);
        ProductCatCollection _ProductCatCollection = ProductCatService.GetProductCatListByParam("[" + ProductCat._Table + "].[" + ProductCatColumns.SortOrder + "]", OrderDirection.ASC, cri.Criter);
        if (_ProductCatCollection.Count > 0)
        {
            foreach (ProductCat _ProductCat in _ProductCatCollection)
            {
                ddlProSubCat.Items.Add(new ListItem(_ProductCat.Name, _ProductCat.ProCateID.ToString()));
            }
        }
    }
    protected void ddlProSubCat_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        ddlProSubCat.Items.Clear();
        ddlProSubCat.Items.Add(new ListItem("- - - -Select SubCategory- - - -", "0"));
        Criteria cri = new Criteria();
        cri.add("[" + ProductCat._Table + "].[" + ProductCatColumns.CatLevel + "]", 1, Condition.EQUAL, Condition.AND);
        cri.add("[" + ProductCat._Table + "].[" + ProductCatColumns.Parrentid + "]", Utility.ParseToBigint(ddlCat.SelectedValue), Condition.EQUAL, Condition.AND);
        ProductCatCollection _ProductCatCollection = ProductCatService.GetProductCatListByParam("[" + ProductCat._Table + "].[" + ProductCatColumns.SortOrder + "]", OrderDirection.ASC, cri.Criter);
        if (_ProductCatCollection.Count > 0)
        {
            foreach (ProductCat _ProductCat in _ProductCatCollection)
            {
                ddlProSubCat.Items.Add(new ListItem(_ProductCat.Name, _ProductCat.ProCateID.ToString()));
            }
        }
    }
    protected void ddlProSubSubCat_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        ddlProSubSubCat.Items.Clear();
        ddlProSubSubCat.Items.Add(new ListItem("- - - -Select SubSubCategory- - - -", "0"));
        Criteria cri = new Criteria();
        cri.add("[" + ProductCat._Table + "].[" + ProductCatColumns.CatLevel + "]", 2, Condition.EQUAL, Condition.AND);
        cri.add("[" + ProductCat._Table + "].[" + ProductCatColumns.Parrentid + "]", Utility.ParseToBigint(ddlProSubCat.SelectedValue), Condition.EQUAL, Condition.AND);
        ProductCatCollection _ProductCatCollection = ProductCatService.GetProductCatListByParam("[" + ProductCat._Table + "].[" + ProductCatColumns.SortOrder + "]", OrderDirection.ASC, cri.Criter);
        if (_ProductCatCollection.Count > 0)
        {
            foreach (ProductCat _ProductCat in _ProductCatCollection)
            {
                ddlProSubSubCat.Items.Add(new ListItem(_ProductCat.Name, _ProductCat.ProCateID.ToString()));
            }
        }
        //LoadSubCatPro();

    }
    #endregion Load about dropdownlist
    #region LoadCurent
    private void LoadCurent()
    {
        HtmlGenericControl ulProduct = (HtmlGenericControl)Master.FindControl("ulProduct");
        ulProduct.Attributes.Add("class", "current");
        HtmlControl divSelectSubPro = (HtmlControl)Master.FindControl("divSelectSubPro");
        divSelectSubPro.Attributes.Add("class", "select_sub show");
        HtmlGenericControl liProListBuy = (HtmlGenericControl)Master.FindControl("liProListBuy");
        liProListBuy.Attributes.Add("class", "sub_show");

    }
    #endregion
    #region LoadData
    private string username = string.Empty;
    private void LoadData()
    {
        Criteria cri = new Criteria();
        hyperlinkPager.PageSize = RTISettings.PageSize;
        NoCountStart = (hyperlinkPager.PageSize * (hyperlinkPager.CurrentIndex - 1)) + 1;
        int totalrecord = 0;
        cri.addJoin(ProductCat._Table, "[" + ProductBuy._Table + "].[" + ProductBuyColumns.CatId + "]", "[" + ProductCat._Table + "].[" + ProductCatColumns.ProCateID + "]", Condition.EQUAL, Condition.LEFT_JOIN);
        if (!string.IsNullOrEmpty(this.Title))
            cri.add("[" + ProductBuy._Table + "].[" + ProductBuyColumns.Title + "]", this.Title, Condition.LIKE, Condition.AND);
        if (!string.IsNullOrEmpty(this.KeyWord))
            cri.add("[" + ProductBuy._Table + "].[" + ProductBuyColumns.KeyWord + "]", this.KeyWord, Condition.LIKE, Condition.AND);
        if (this.CatPro != long.MinValue && this.CatPro != 0 && this.SubCatPro == long.MinValue && this.SubCatPro == 0 && this.SubSubCatPro == long.MinValue && this.SubSubCatPro == 0)
        {
            cri.add("[" + ProductCat._Table + "].[" + ProductCatColumns.Parrentid + "]", this.CatPro, Condition.EQUAL, Condition.AND);
        }
        if (this.SubCatPro != long.MinValue && this.SubCatPro != 0 && this.SubSubCatPro == long.MinValue && this.SubSubCatPro == 0)
        {
            cri.add("[" + ProductCat._Table + "].[" + ProductCatColumns.Parrentid + "]", this.SubCatPro, Condition.EQUAL, Condition.AND);
        }
        if (this.SubSubCatPro != long.MinValue && this.SubSubCatPro != 0)
        {
            cri.add("[" + ProductBuy._Table + "].[" + ProductBuyColumns.CatId + "]", this.SubSubCatPro, Condition.EQUAL, Condition.AND);
        }
        ProductBuyCollection _ProductBuyCollection = ProductBuyService.GetProductBuyListByParam(hidColSort, OrderDirection.ASC, hyperlinkPager.CurrentIndex, hyperlinkPager.PageSize, cri.Criter, out totalrecord);

        rptList.DataSource = _ProductBuyCollection;
        rptList.DataBind();
        hyperlinkPager.ItemCount = totalrecord;
        if ((float)totalrecord / (float)hyperlinkPager.PageSize <= 1)
            hyperlinkPager.Visible = false;

    }
    protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            ProductBuy _ProductBuy = (ProductBuy)e.Item.DataItem;
            HiddenField hidID = (HiddenField)e.Item.FindControl("hidID");
            HtmlTableRow tr = (HtmlTableRow)e.Item.FindControl("tr");
            Literal ltrTitleChild = (Literal)e.Item.FindControl("ltrTitleChild");
            Literal ltrSubCatTitle = (Literal)e.Item.FindControl("ltrSubCatTitle");
            Literal ltrCatTitle = (Literal)e.Item.FindControl("ltrCatTitle");
            HtmlAnchor hrfSubCat = (HtmlAnchor)e.Item.FindControl("hrfSubCat");
            HtmlAnchor hrfCat = (HtmlAnchor)e.Item.FindControl("hrfCat");
            TextBox txtOrder = (TextBox)e.Item.FindControl("txtOrder");
            CheckBox ckEnable = (CheckBox)e.Item.FindControl("ckEnable");
            CheckBox ckapproved = (CheckBox)e.Item.FindControl("ckapproved");
            CheckBox ckIsdelete = (CheckBox)e.Item.FindControl("ckIsdelete");
            Literal ltrMember = (Literal)e.Item.FindControl("ltrMember");
            Literal ltrCrateDate = (Literal)e.Item.FindControl("ltrCrateDate");
            Literal ltrTitle = (Literal)e.Item.FindControl("ltrTitle");
            ltrTitle.Text = _ProductBuy.Title;
            ProductCat _ProductCatSubSub = ProductCatService.GetProductCat(_ProductBuy.CatId);
            if (_ProductCatSubSub != null)
            {
                ltrTitleChild.Text = _ProductCatSubSub.Name;
                ProductCat _ProductCatSub = ProductCatService.GetProductCat(_ProductCatSubSub.Parrentid);
                if (_ProductCatSub != null)
                {
                    hrfSubCat.HRef = "ProductCatSub.aspx?Id=" + _ProductCatSub.ProCateID + "";
                    ltrSubCatTitle.Text = _ProductCatSub.Name;
                    ProductCat _CatProductCat = ProductCatService.GetProductCat(_ProductCatSub.Parrentid);
                    if (_CatProductCat != null)
                    {
                        hrfCat.HRef = "ProductCat.aspx?Id=" + _CatProductCat.ProCateID + "";
                        ltrCatTitle.Text = _CatProductCat.Name;
                    }
                }
            }
            Member _Member = MemberService.GetMember(_ProductBuy.MemberId);
            if (_Member != null)
            {
                ltrMember.Text = _Member.FullName;
            }
            ltrCrateDate.Text = Utility.GetFormattedDate(_ProductBuy.CreatedDate);
            txtOrder.Text = _ProductBuy.Order.ToString();
            ckEnable.Checked = _ProductBuy.Enable;
            ckapproved.Checked = _ProductBuy.Approved;
            ckIsdelete.Checked = _ProductBuy.Not_Clear;
            hidID.Value = _ProductBuy.ProductId.ToString();
            int STT = e.Item.ItemIndex + 1;
            Literal ltrNo = (Literal)e.Item.FindControl("ltrNo");
            ltrNo.Text = STT.ToString();
            if (STT % 2 != 0)
            {
                tr.Attributes["class"] = "alternate-row";
            }
            else
            {
                tr.Attributes["class"] = "none";
            }
        }

    }
    protected void pager_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
        hyperlinkPager.CurrentIndex = currnetPageIndx;
        LoadData();
    }
    protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string commandName = e.CommandName;
        object obj = new object();
        switch (commandName)
        {
            case "sortName":
                obj = "[" + ProductCat._Table + "].[" + ProductCatColumns.Name + "]";
                break;
            case "SortOrder":
                obj = "[" + ProductCat._Table + "].[" + ProductCatColumns.SortOrder + "]";
                break;
            default:
                break;
        }
        if (OrderDirection.ASC == OrderDirection.ASC)
            Session["Order"] = OrderDirection.DESC;
        else
            Session["Order"] = OrderDirection.ASC;
        this.hidColSort = obj.ToString();
        LoadData();
    }
    #endregion
    #region Delete Account
    protected void lndelete_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in this.rptList.Items)
        {
            HtmlInputCheckBox chkDelete = (HtmlInputCheckBox)item.FindControl("chkDelete");
            if (chkDelete.Checked == true)
            {
                HiddenField hidID = (HiddenField)item.FindControl("hidID");
                string strhidID = hidID.Value;
                long ID = long.Parse(strhidID);

                try
                {
                    ProductBuyService.DeleteProductBuy(ID);
                }
                catch
                {
                    lb_error.Text = "Dont's delete this item because it is getting using another items";
                }
            }
        }
        LoadData();
    }
    #endregion
    #region Event button
    protected void btnSave_OnClick(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in this.rptList.Items)
        {
            HiddenField hidID = (HiddenField)item.FindControl("hidID");
            CheckBox ckEnable = (CheckBox)item.FindControl("ckEnable");
            CheckBox ckapproved = (CheckBox)item.FindControl("ckapproved");
            CheckBox ckIsdelete = (CheckBox)item.FindControl("ckIsdelete");
            ProductBuy _ProductBuy = ProductBuyService.GetProductBuy(long.Parse(hidID.Value));
            TextBox txtOrder = (TextBox)item.FindControl("txtOrder");

            if (_ProductBuy != null)
            {
                _ProductBuy.Order = long.Parse(txtOrder.Text.Trim());
                _ProductBuy.Enable = ckEnable.Checked;
                _ProductBuy.Approved = ckapproved.Checked;
                _ProductBuy.Not_Clear = ckIsdelete.Checked;
                ProductBuyService.UpdateProductBuy(_ProductBuy);
            }
        }
        LoadData();
    }
    protected void btnSearch_OnClick(object sender, EventArgs e)
    {
        Response.Redirect(string.Format("ProductbuyList.aspx?Title={0}&CatPro={1}&SubCatPro={2}&SubSubCatPro={3}&KeyWord={4}", txtSearch.Value.Trim(), ddlCat.SelectedValue, ddlProSubCat.SelectedValue, ddlProSubSubCat.SelectedValue, txtKeyWord.Text.Trim()));
    }
    protected void btnSearchAll_OnClick(object sender, EventArgs e)
    {
        Response.Redirect("ProductbuyList.aspx");
    }
    #endregion
}
